package com.mohe.dangjian.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.OrgStructure;
import com.mohe.dangjian.service.interfaces.IOrgStructure;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 居民区组织架构Action
 * 
 * @author wxm
 * 
 */
public class CommunityOrgStructureAction extends ActionSupport {

	private static final long serialVersionUID = 1754621042747865345L;
	private static final String partyType = "居民区";

	private IOrgStructure orgStructureServ;

	private List<OrgStructure> orgStructureList;
	private Page page = new Page();
	private OrgStructure orgStructure = new OrgStructure();
	
	// 消息提示
	private String flag;

	public IOrgStructure getOrgStructureServ() {
		return orgStructureServ;
	}

	public void setOrgStructureServ(IOrgStructure orgStructureServ) {
		this.orgStructureServ = orgStructureServ;
	}

	public List<OrgStructure> getOrgStructureList() {
		return orgStructureList;
	}

	public void setOrgStructureList(List<OrgStructure> orgStructureList) {
		this.orgStructureList = orgStructureList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public OrgStructure getOrgStructure() {
		return orgStructure;
	}

	public void setOrgStructure(OrgStructure orgStructure) {
		this.orgStructure = orgStructure;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
	 * 获取组织架构信息列表
	 * 
	 * @return
	 */
	public String getOrgStructureListForPage() {
		orgStructure.setPartyType(partyType);
		int count = orgStructureServ.getCount(orgStructure);
		page.setCount(count);
		orgStructureList = orgStructureServ.getOrgStructureList(orgStructure, page);
		return "orgStructure.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchOrgStructureList() {
		orgStructure.setPartyType(partyType);
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = orgStructureServ.getCount(orgStructure);
		page.setCount(count);
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<OrgStructure> list = orgStructureServ.getOrgStructureList(orgStructure, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (OrgStructure s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getGeneralBranch())) {
					map.put("generalBranch", s.getGeneralBranch());
				} else {
					map.put("generalBranch", "");
				}
				if (!StringUtils.isBlank(s.getBranch())) {
					map.put("branch", s.getBranch());
				} else {
					map.put("branch", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	/**
	 * 跳转到编辑界面
	 * 
	 * @return
	 */
	public String toEditOrgStructure() {
		orgStructure = orgStructureServ.getOrgStructureById(orgStructure.getTimeKey());
		return "editOrgStructure.jsp";
	}
	
	/**
	 * 更新组织架构信息
	 */
	public void updateOrgStructure(){
		orgStructure.setPartyType(partyType);
		Json json = new Json();
		orgStructure.setCreateDate(new Date());
		boolean result = orgStructureServ.updateOrgStructure(orgStructure);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到新增界面
	 * 
	 * @return
	 */
	public String toAddOrgStructure() {
		return "addOrgStructure.jsp";
	}

	/**
	 * 保存新增党员
	 */
	public void saveOrgStructure() {
		orgStructure.setPartyType(partyType);
		Json json = new Json();
		boolean result = orgStructureServ.saveOrgStructure(orgStructure);
		if (result) {
			json.setMessage("新增成功");
		} else {
			json.setSuccess(true);
			json.setMessage("新增失败");
		}
		JsonUtil.writeJson(json);
	}

	/**
	 * 删除组织架构
	 */
	public void delOrgStructure() {
		Json json = new Json();
		boolean result = orgStructureServ.delOrgStructure(orgStructure.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(true);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String toShowOrgStructure(){
		orgStructure = orgStructureServ.getOrgStructureById(orgStructure.getTimeKey());
		return "showOrgStructure.jsp";
	}
}
